#!/bin/bash

# Flutter 应用图标生成脚本
#
# 使用方式1: 修改下面的变量，然后直接运行 ./generate_app_icon.sh
# 使用方式2: 命令行参数 ./generate_app_icon.sh <源图标> <目标项目>

set -e

# ==================== 配置区域 ====================
# 修改这里的变量来指定图标和项目

# 源图标文件名（相对于脚本所在目录）
ICON_NAME="icon/todo.png"

# 目标项目路径（相对于脚本所在目录）
PROJECT_NAME="../sloth-bear-todo"

# ==================== 配置结束 ====================

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

# 如果有命令行参数，则使用命令行参数
if [ $# -ge 2 ]; then
    ICON_NAME="$1"
    PROJECT_NAME="$2"
fi

SOURCE_ICON="$ICON_NAME"
TARGET_PROJECT="$PROJECT_NAME"

# 如果源图标是相对路径，则相对于脚本目录
if [[ ! "$SOURCE_ICON" = /* ]]; then
    SOURCE_ICON="$SCRIPT_DIR/$SOURCE_ICON"
fi

# 如果目标项目是相对路径，则相对于脚本目录
if [[ ! "$TARGET_PROJECT" = /* ]]; then
    TARGET_PROJECT="$SCRIPT_DIR/$TARGET_PROJECT"
fi

# 检查源图标是否存在
if [ ! -f "$SOURCE_ICON" ]; then
    echo "错误: 源图标文件不存在: $SOURCE_ICON"
    exit 1
fi

# 检查目标项目是否存在
if [ ! -d "$TARGET_PROJECT" ]; then
    echo "错误: 目标项目目录不存在: $TARGET_PROJECT"
    exit 1
fi

# 检查是否安装了 sips (macOS 自带)
if ! command -v sips &> /dev/null; then
    echo "错误: 需要 sips 命令 (macOS 自带)"
    exit 1
fi

echo "源图标: $SOURCE_ICON"
echo "目标项目: $TARGET_PROJECT"
echo ""

# ==================== iOS 图标 ====================
IOS_ICON_DIR="$TARGET_PROJECT/ios/Runner/Assets.xcassets/AppIcon.appiconset"

if [ -d "$IOS_ICON_DIR" ]; then
    echo "生成 iOS 图标..."

    # iOS 图标尺寸映射 (文件名 -> 尺寸)
    declare -a IOS_ICONS=(
        "Icon-App-20x20@1x.png:20"
        "Icon-App-20x20@2x.png:40"
        "Icon-App-20x20@3x.png:60"
        "Icon-App-29x29@1x.png:29"
        "Icon-App-29x29@2x.png:58"
        "Icon-App-29x29@3x.png:87"
        "Icon-App-40x40@1x.png:40"
        "Icon-App-40x40@2x.png:80"
        "Icon-App-40x40@3x.png:120"
        "Icon-App-50x50@1x.png:50"
        "Icon-App-50x50@2x.png:100"
        "Icon-App-57x57@1x.png:57"
        "Icon-App-57x57@2x.png:114"
        "Icon-App-60x60@2x.png:120"
        "Icon-App-60x60@3x.png:180"
        "Icon-App-72x72@1x.png:72"
        "Icon-App-72x72@2x.png:144"
        "Icon-App-76x76@1x.png:76"
        "Icon-App-76x76@2x.png:152"
        "Icon-App-83.5x83.5@2x.png:167"
        "Icon-App-1024x1024@1x.png:1024"
    )

    for item in "${IOS_ICONS[@]}"; do
        filename="${item%%:*}"
        size="${item##*:}"
        target_path="$IOS_ICON_DIR/$filename"

        echo "  生成 $filename (${size}x${size})"
        sips -z "$size" "$size" "$SOURCE_ICON" --out "$target_path" > /dev/null 2>&1
    done

    echo "iOS 图标生成完成!"
else
    echo "警告: iOS 图标目录不存在，跳过 iOS 图标生成"
fi

echo ""

# ==================== Android 图标 ====================
ANDROID_RES_DIR="$TARGET_PROJECT/android/app/src/main/res"

if [ -d "$ANDROID_RES_DIR" ]; then
    echo "生成 Android 图标..."

    # Android 图标尺寸映射 (目录 -> 尺寸)
    declare -a ANDROID_ICONS=(
        "mipmap-mdpi:48"
        "mipmap-hdpi:72"
        "mipmap-xhdpi:96"
        "mipmap-xxhdpi:144"
        "mipmap-xxxhdpi:192"
    )

    for item in "${ANDROID_ICONS[@]}"; do
        dir="${item%%:*}"
        size="${item##*:}"
        target_path="$ANDROID_RES_DIR/$dir/ic_launcher.png"

        # 确保目录存在
        mkdir -p "$ANDROID_RES_DIR/$dir"

        echo "  生成 $dir/ic_launcher.png (${size}x${size})"
        sips -z "$size" "$size" "$SOURCE_ICON" --out "$target_path" > /dev/null 2>&1
    done

    echo "Android 图标生成完成!"
else
    echo "警告: Android 资源目录不存在，跳过 Android 图标生成"
fi

echo ""

# ==================== macOS 图标 ====================
MACOS_ICON_DIR="$TARGET_PROJECT/macos/Runner/Assets.xcassets/AppIcon.appiconset"

if [ -d "$MACOS_ICON_DIR" ]; then
    echo "生成 macOS 图标..."

    # macOS 图标尺寸映射 (文件名 -> 尺寸)
    declare -a MACOS_ICONS=(
        "app_icon_16.png:16"
        "app_icon_32.png:32"
        "app_icon_64.png:64"
        "app_icon_128.png:128"
        "app_icon_256.png:256"
        "app_icon_512.png:512"
        "app_icon_1024.png:1024"
    )

    for item in "${MACOS_ICONS[@]}"; do
        filename="${item%%:*}"
        size="${item##*:}"
        target_path="$MACOS_ICON_DIR/$filename"

        echo "  生成 $filename (${size}x${size})"
        sips -z "$size" "$size" "$SOURCE_ICON" --out "$target_path" > /dev/null 2>&1
    done

    echo "macOS 图标生成完成!"
else
    echo "警告: macOS 图标目录不存在，跳过 macOS 图标生成"
fi

echo ""
echo "=========================================="
echo "所有图标生成完成!"
echo "=========================================="
